﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace CustomTokenDemo
{
    public class ApiAuthorize : IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationFilterContext context)
        {
            if (context.Filters.Contains(new NoAuthentication()))
                return;
            var authorize = context.HttpContext.Request.Headers["MyAuthentication"];

            if (string.IsNullOrWhiteSpace(authorize))
            {
                context.Result = new JsonResult("请求authorize不能为空!");
                return;
            }

            if(!MemoryCacheHelper.Exists(authorize))
            {
                context.Result = new JsonResult("无效的授权信息或授权信息已过期!");
                return;
            }

        }
    }

    //IFilterMetadata:MVC 请求管道中处理的筛选器的标记接口
    public class MyAuthentication : Attribute, IFilterMetadata { }

    public class NoAuthentication : Attribute, IFilterMetadata { }



}
